package controllers

import (
	session "HomeStation/controllers"
	db "HomeStation/db"
	utils "HomeStation/utils"
	"fmt"

	"github.com/astaxie/beego/orm"
	beego "github.com/beego/beego/v2/server/web"
)

type DeviceController struct {
	beego.Controller
}

func (this *DeviceController) DeviceOnline() {
	var response session.Response
	var err error
	user := session.CheckUserOfSession(&this.Controller)
	if nil == user {
		response = session.Response{Code: 304, Message: "请登录"}
		this.Data["json"] = &response
		this.ServeJSON()
		return
	}

	err = utils.CheckRequestParams(&this.Controller, "deviceId")
	if nil != err {
		response = session.Response{Code: 0, Message: fmt.Sprintf("缺少参数: %v", err)}
		this.Data["json"] = &response
		this.ServeJSON()
		return
	}

	deviceId, _ := this.GetInt("deviceId", -1)
	if deviceId < 0 {
		response = session.Response{Code: 0, Message: "deviceId 参数错误"}
		this.Data["json"] = &response
		this.ServeJSON()
		return
	}
	device := db.Device{Id: deviceId}
	o := orm.NewOrm()
	o.QueryTable("device").Filter("User", user).Filter("Id", deviceId).One(&device)
	ip := device.Ip
	if len(ip) <= 0 {
		response = session.Response{Code: 0, Message: fmt.Sprintf("设备信息错误 ip: %s", ip)}
		this.Data["json"] = &response
		this.ServeJSON()
		return
	} else {
		isOnline := utils.GetDeviceIsOnline(ip)
		response = session.Response{Code: 200, Message: "查询成功", Data: map[string]interface{}{
			"name":       device.Name,
			"deviceType": device.DeviceType,
			"id":         device.Id,
			"isOnline":   isOnline,
		}}
		this.Data["json"] = &response
		this.ServeJSON()
	}

}

///添加设备
func (this *DeviceController) AddDevice() {
	UpdataOrInsert(this, false)
}

///更新设备
func (this *DeviceController) UpdateDevice() {
	UpdataOrInsert(this, true)
}

////更新 或者新增设备
func UpdataOrInsert(this *DeviceController, update bool) {
	var err error
	user := session.CheckUserOfSession(&this.Controller)
	var response session.Response
	if nil == user {
		response = session.Response{Code: 304, Message: "查询失败 请先登录"}
		this.Data["json"] = &response
		this.ServeJSON()
		return
	}
	err = utils.CheckRequestParams(&this.Controller, "deviceName", "deviceType", "deviceIp", "macAddress")
	if nil != err {
		response = session.Response{Code: 0, Message: fmt.Sprintf("%v", err)}
		this.Data["json"] = &response
		this.ServeJSON()
		return
	}

	deviceName := this.GetString("deviceName")
	deviceType := this.GetString("deviceType")
	macAddress := this.GetString("macAddress")
	deviceIp := this.GetString("deviceIp")

	device := db.Device{Name: deviceName, User: user}
	o := orm.NewOrm()
	err = o.Read(&device, "Name", "User")

	device.DeviceType = deviceType
	device.Ip = deviceIp
	device.MacAddress = macAddress

	if nil != err {
		_, err := o.Insert(&device)
		if nil != err {
			fmt.Println(err)
		}

		response = session.Response{Code: 200, Message: "添加成功"}
		this.Data["json"] = &response
		this.ServeJSON()
	} else {
		if update {
			_, err = o.Update(&device)
			if nil == err {
				response = session.Response{Code: 200, Message: "更新成功", Data: device}
				this.Data["json"] = &response
				this.ServeJSON()
				return
			} else {
				response = session.Response{Code: 0, Message: "更新失败"}
				this.Data["json"] = &response
				this.ServeJSON()
				return
			}
		} else {
			response = session.Response{Code: 0, Message: "已存在相同设备 请勿重复添加"}
			this.Data["json"] = &response
			this.ServeJSON()
		}
	}
}

func (this *DeviceController) GetAllDevices() {
	var response session.Response
	user := session.CheckUserOfSession(&this.Controller)
	if nil == user {
		response = session.Response{Code: 304, Message: "查询失败 请先登录"}
		this.Data["json"] = &response
		this.ServeJSON()
		return
	}
	fmt.Println(fmt.Sprintf("当前登录用户：%v", user))

	var devices []*db.Device
	var err error
	o := orm.NewOrm()
	_, err = o.QueryTable("device").Filter("User", user).All(&devices, "Id", "Name", "DeviceType", "Ip", "MacAddress")
	if nil != err {
		response = session.Response{Code: 0, Message: fmt.Sprintf("查询失败 %v", err)}
		this.Data["json"] = &response
		this.ServeJSON()
		return
	} else {
		response = session.Response{Code: 200, Message: "查询成功", Data: devices}
		this.Data["json"] = &response
		this.ServeJSON()
	}
}
